﻿using DevExpress.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Timesheet.Domain;
using Timesheet.Service;

namespace Timesheet.WebForm.Projects
{
    public partial class ProjectMrgCoord:System.Web.UI.Page
    {
        public IProjectService _ProjectService { get; set; }
        public ILocationsService _LocationsService { get; set; }
        public IDisciplinesService _DisciplinesService { get; set; }
        public IUserService _UserService { get; set; }
        public ITeamService _TeamService { get; set; }
        public ITeamDetailService _TeamDetailService { get; set; }
        public IPrjReferencesService _PrjReferencesService { get; set; }

        Project _CurrentPrj = new Project();
        protected void Page_Load(object sender,EventArgs e)
        {
            if(!IsPostBack)
            {
                var user = (User)Session["User"];
                if (user == null)
                    Response.Redirect("~/Account/Login.aspx");
                _CurrentPrj = _ProjectService.GetByID(Convert.ToInt64(Request.Params["id"]));
                if(_CurrentPrj != null)
                {
                    lblProjectName.Value = _CurrentPrj.ProjectName;
                    lblCustomer.Value = _CurrentPrj.CustomerInfo;
                    lblJob.Value = _CurrentPrj.JobInfo;
                    lblJobDescription.Value = _CurrentPrj.JobDes;
                    lblContact.Value = _CurrentPrj.Contact;
                    lblNetwork.Value = _CurrentPrj.NetCode;
                    lblWbs.Value = _CurrentPrj.Wbs;
                    lblResp.Value = _CurrentPrj.ResponsePerson;
                    lblTicket.Value = (_CurrentPrj.JobInfo ?? "") + "-" + _CurrentPrj.Ticket;
                    lblActive.Value = _CurrentPrj.Active ? "Active":"";
                }
            }
        }

        protected void dsLocations_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _PrjReferencesService;
        }

        protected void dsDisciplines_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _DisciplinesService;
        }

        protected void dsTeams_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _TeamService;
        }

        protected void dsEmployers_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _UserService;
        }

        protected void dsTeamDetails_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _TeamDetailService;
        }

        protected void grvTeamDetails_BeforePerformDataSelect(object sender,EventArgs e)
        {
            Session["teamID"] = (sender as ASPxGridView).GetMasterRowKeyValue();            
        }

        protected void btnNewLead_Click(object sender,EventArgs e)
        {
            //var masterKey = grvTeams.GetMasterRowKeyValue();
            //ASPxGridView detailG = grvTeams.FindRowTemplateControlByKey(masterKey,"grvTeamDetails") as ASPxGridView;
            //detailG.AddNewRow();
        }

        protected void btnNewTeam_Click(object sender,EventArgs e)
        {
            grvTeams.AddNewRow();
        }

        protected void grvTeams_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["ProjectID"] = Convert.ToInt64(Request.Params["id"]);
        }

        protected void grvTeamDetails_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["TeamID"] = Convert.ToInt64(Session["teamID"]);
        }

        protected void grvTeamDetails_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            e.NewValues["TeamID"] = Convert.ToInt64(Session["teamID"]);
        }

        protected void grvTeams_RowUpdating(object sender,DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {            
            e.NewValues["ProjectID"] = Convert.ToInt64(Request.Params["id"]);
        }
    }
}